/*set working directory*/
/*ssc install blindschemes*/

/*Robustness for Figure 3.6*/

/*Set-Up Data for Analysis*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
generate density_sq=density*density
collapse (mean) density density_sq, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace density_sq=0 if _merge==2
replace year=year+1 /*to lag density*/
drop if year>2019
drop _merge
sort field year
tempfile density
save `density'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein: gen dup = cond(_N==1, 0, _n)
drop if dup>1 & ein!=.
drop dup
sort name fisyr
quietly by name: gen dup = cond(_N==1, 0, _n)
drop if dup>1 
drop dup
destring ruledate, replace
drop if ruledate==. | ruledate<190000
generate foundingyear=int(ruledate/100)
destring fisyr, replace
generate founded=1
by field foundingyear, sort: generate foundings=sum(founded)
collapse (max) foundings, by(field foundingyear)
label variable foundings "Number"
generate year=foundingyear
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace foundings=0 if _merge==2
drop _merge
sort field year
merge 1:1 field year using `density'
drop if _merge==1
drop _merge
sort field year
merge field year using "Data/US Aid by Sector Year Combined.dta"
drop _merge
sort field year 
merge field year using "Data/congress.dta"
drop _merge
sort field year 
merge field year using "Data/media.dta"
drop _merge
drop if year<1992
egen stddensity=std(density)
egen stddensity_sq=std(density_sq)
generate lnaid=log(total_aid+1)
label variable stddensity "Density"
label variable stddensity_sq "Density Squared"
label variable lnaid "U.S. Aid"
label variable lnmedia "Media Attention"
label variable lncongress "Policy Attention"
sort field year
tempfile mergeddata
save `mergeddata'

/*Table for Figure 3.6*/
reg foundings lnaid lnmedia lncongress, vce(cluster field)
outreg2 using "Tables/figure3-6.tex", ctitle(Baseline Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) replace
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq, vce(cluster field)
outreg2 using "Tables/figure3-6.tex", ctitle(Full Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) append

/*Control for Population Age*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
destring ruledate, replace
drop if ruledate<180000
sort field ruledate
by field, sort: gen count = _n
keep if count==1
generate foundingyear=int(ruledate/100)
sort field
merge 1:m field using "Data/yearlist_1990-2019.dta"
generate age=year-foundingyear
label variable age "Population Age"
keep field year age
sort field year
merge 1:1 field year using `mergeddata'
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq age, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_Population_Age.pdf", as(pdf) replace

/*Concentration robustness check*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
by field fisyr: generate top_4 = _n > (_N - 4)
collapse (sum) totrev if top_4==1, by(field fisyr)
rename totrev top4rev
sort field fisyr
tempfile top4
save `top4'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
collapse (sum) totrev, by(field fisyr)
bysort field: gen lag_totrev = totrev[_n-1]
generate pct_change = (totrev - lag_totrev) / lag_totrev * 100
generate abs_pct_change = abs(pct_change)
sort field fisyr
merge 1:1 field fisyr using `top4'
drop _merge
generate concentration4firm=top4rev/totrev
rename fisyr year
replace year=year+1 /*to lag concentration*/
sort field year
merge 1:1 field year using `mergeddata'
label variable concentration4firm "Concentration"
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq concentration4firm, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_concentration.pdf", as(pdf) replace

/*Republican control of Congress robustness check*/
generate repub_congress=1
replace repub_congress=0 if year==1993 | year==1994 | year==2007 | year==2008 | year==2009 | year==2010 | year ==2019
label variable repub_congress "GOP Control"
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq repub_congress, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_Rep_Control.pdf", as(pdf) replace

/*Post-9/11 robustness check*/
generate Sept11=0
replace Sept11=1 if year>2011
label variable Sept11 "Post-9/11"
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq Sept11, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_911.pdf", as(pdf) replace

/*Year Indicator robustness check*/
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq i.year, vce(cluster field)
estimates store Full
coefplot Baseline Full, keep(*lnaid *lnmedia *lncongress *stddensity *stddensity_sq) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_Year_Indicators.pdf", as(pdf) replace

/*Negative Binomial Models*/
nbreg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
nbreg foundings lnaid lnmedia lncongress stddensity stddensity_sq, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_Negative_Binomial.pdf", as(pdf) replace

/*Total number of INGOs robustness check*/
clear
import excel "Data/NGOs_AllCountries", sheet("Sheet1") firstrow
drop if Founded==.
foreach num of numlist 1900/2022 {
	gen v_`num'=`num'
	} 
foreach x of varlist v_1900-v_2022 {
    replace `x'=. if `x'<Founded
    }
foreach x of varlist v_1900-v_2022 {
    replace `x'=0 if `x'==.
    }
foreach x of varlist v_1900-v_2022 {
    replace `x'=1 if `x'>0
    }
gen id=_n
reshape long v_, i(id) j(year)	
drop id
rename v_ total
collapse (sum) total, by(year)
replace year=year+1 /*to lag variable*/
sort year
merge 1:m year using `mergeddata'
egen totalingos=std(total)
label variable totalingos "Total INGOs"
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq totalingos, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_Total_INGOs.pdf", as(pdf) replace

/*Average INGO size robustness check*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
*Use calculator from https://www.usinflationcalculator.com/ for constant 2020 USD
generate totrev_inflation=.
replace totrev_inflation=totrev*1.012 if fisyr==2019
replace totrev_inflation=totrev*1.031 if fisyr==2018
replace totrev_inflation=totrev*1.056 if fisyr==2017
replace totrev_inflation=totrev*1.078 if fisyr==2016
replace totrev_inflation=totrev*1.092 if fisyr==2015
replace totrev_inflation=totrev*1.093 if fisyr==2014
replace totrev_inflation=totrev*1.111 if fisyr==2013
replace totrev_inflation=totrev*1.127 if fisyr==2012
replace totrev_inflation=totrev*1.151 if fisyr==2011
replace totrev_inflation=totrev*1.187 if fisyr==2010
replace totrev_inflation=totrev*1.206 if fisyr==2009
replace totrev_inflation=totrev*1.202 if fisyr==2008
replace totrev_inflation=totrev*1.248 if fisyr==2007
replace totrev_inflation=totrev*1.284 if fisyr==2006
replace totrev_inflation=totrev*1.325 if fisyr==2005
replace totrev_inflation=totrev*1.370 if fisyr==2004
replace totrev_inflation=totrev*1.407 if fisyr==2003
replace totrev_inflation=totrev*1.439 if fisyr==2002
replace totrev_inflation=totrev*1.462 if fisyr==2001
replace totrev_inflation=totrev*1.503 if fisyr==2000
replace totrev_inflation=totrev*1.553 if fisyr==1999
replace totrev_inflation=totrev*1.588 if fisyr==1998
replace totrev_inflation=totrev*1.613 if fisyr==1997
replace totrev_inflation=totrev*1.650 if fisyr==1996
replace totrev_inflation=totrev*1.698 if fisyr==1995
replace totrev_inflation=totrev*1.746 if fisyr==1994
replace totrev_inflation=totrev*1.791 if fisyr==1993
replace totrev_inflation=totrev*1.845 if fisyr==1992
replace totrev_inflation=totrev*1.900 if fisyr==1991
replace totrev_inflation=totrev*1.980 if fisyr==1990
destring fisyr, replace
collapse (sum) totrev_inflation, by(field fisyr)
generate constantmillions=(totrev_inflation/1000000)
label variable constantmillions "Dollars (Millions)"
rename fisyr year
replace year=year+1 /*to lag variable*/
sort field year
merge 1:1 field year using `mergeddata'
generate averageingo=constantmillions/density
label variable averageingo "INGO Size"
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq averageingo, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_INGO_Size.pdf", as(pdf) replace

/*Average level of democracy in the world*/
use "Data/V-Dem-CY-Full+Others-v12.dta", clear
keep if year>1990 & year<2019
collapse (mean) v2x_polyarchy, by(year)
label variable v2x_polyarchy "Democracy"
replace year=year+1 /*to lag variable*/
sort year
merge 1:m year using `mergeddata'
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq v2x_polyarchy, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_Democracy.pdf", as(pdf) replace

/*IGOs robustness check*/
clear
import excel "Data/igos.xlsx", sheet("Sheet1") firstrow
rename conservationigototal igos
rename field fieldalt
generate field=.
replace field=2 if fieldalt=="Relief"
replace field=3 if fieldalt=="Democracy"
replace field=4 if fieldalt=="Environment"
replace field=5 if fieldalt=="Health"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
label variable igos "IGOs"
sort field year
merge 1:1 field year using `mergeddata'
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq igos, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922012_IGOs.pdf", as(pdf) replace

/*Private Giving*/
drop _merge
sort year 
merge year using "Data/Private_Giving.dta"
label variable giving_intl "Private Giving"
reg foundings giving_intl lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings giving_intl lnmedia lncongress stddensity stddensity_sq, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_Private_Giving.pdf", as(pdf) replace

/*TNA Access*/
use "Data/Transaccess data.dta", clear
rename Year year
collapse (mean) access, by(year)
replace year=year+1
label variable access "Access to IGOs"
merge 1:m year using `mergeddata'
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq access, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922009_INGO_Access.pdf", as(pdf) replace

/*Cluster standard errors on year as well as field*/
reg foundings lnaid lnmedia lncongress, vce(cluster field year)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq, vce(cluster field year)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_ClusteredSEsYear.pdf", as(pdf) replace

/*Control for year*/
label variable year "Year"
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq year, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_YearControl.pdf", as(pdf) replace

/*2, 4, and 6 year density lags*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
generate density_sq=density*density
collapse (mean) density density_sq, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace density_sq=0 if _merge==2
replace year=year+2 /*to lag density*/
drop if year>2019
drop _merge
rename density density_2yearslag
rename density_sq density_2yearslag_sq
sort field year
tempfile density_2yearslag
save `density_2yearslag'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
generate density_sq=density*density
collapse (mean) density density_sq, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace density_sq=0 if _merge==2
replace year=year+4 /*to lag density*/
drop if year>2019
drop _merge
rename density density_4yearslag
rename density_sq density_4yearslag_sq
sort field year
tempfile density_4yearslag
save `density_4yearslag'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
generate density_sq=density*density
collapse (mean) density density_sq, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace density_sq=0 if _merge==2
replace year=year+6 /*to lag density*/
drop if year>2019
drop _merge
rename density density_6yearslag
rename density_sq density_6yearslag_sq
sort field year
tempfile density_6yearslag
save `density_6yearslag'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein: gen dup = cond(_N==1, 0, _n)
drop if dup>1 & ein!=.
drop dup
sort name fisyr
quietly by name: gen dup = cond(_N==1, 0, _n)
drop if dup>1 
drop dup
destring ruledate, replace
drop if ruledate==. | ruledate<190000
generate foundingyear=int(ruledate/100)
destring fisyr, replace
generate founded=1
by field foundingyear, sort: generate foundings=sum(founded)
collapse (max) foundings, by(field foundingyear)
label variable foundings "Number"
generate year=foundingyear
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace foundings=0 if _merge==2
drop _merge
sort field year
merge 1:1 field year using `density_2yearslag'
drop if _merge==1
drop _merge
sort field year
merge 1:1 field year using `density_4yearslag'
drop if _merge==1
drop _merge
sort field year
merge 1:1 field year using `density_6yearslag'
drop if _merge==1
drop _merge
sort field year
merge field year using "Data/US Aid by Sector Year Combined.dta"
drop _merge
sort field year 
merge field year using "Data/congress.dta"
drop _merge
sort field year 
merge field year using "Data/media.dta"
drop _merge
drop if year<1992
egen stddensity_2yearslag=std(density_2yearslag)
egen stddensity_sq_2yearslag=std(density_2yearslag_sq)
egen stddensity_4yearslag=std(density_4yearslag)
egen stddensity_sq_4yearslag=std(density_4yearslag_sq)
egen stddensity_6yearslag=std(density_6yearslag)
egen stddensity_sq_6yearslag=std(density_6yearslag_sq)
generate lnaid=log(total_aid+1)
label variable stddensity_2yearslag "Density"
label variable stddensity_sq_2yearslag "Density Squared"
label variable stddensity_4yearslag "Density"
label variable stddensity_sq_4yearslag "Density Squared"
label variable stddensity_6yearslag "Density"
label variable stddensity_sq_6yearslag "Density Squared"
label variable lnaid "U.S. Aid"
label variable lnmedia "Media Attention"
label variable lncongress "Policy Attention"
sort field year
tempfile mergeddata
save `mergeddata'

reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity_2yearslag stddensity_sq_2yearslag, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_2yearlag.pdf", as(pdf) replace

reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity_4yearslag stddensity_sq_4yearslag, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_4yearlag.pdf", as(pdf) replace

reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity_6yearslag stddensity_sq_6yearslag, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19922019_6yearlag.pdf", as(pdf) replace

/*Robustness Check using YIO Data on US INGO Foundings*/
clear
import excel "Data/NGOs_AllCountries", sheet("Sheet1") firstrow
keep if CountryTerritoryHQ=="USA"
gen count=1
collapse (sum) count, by(Founded)
label variable count "Foundings"
label variable Founded "Year"
rename Founded year
tempfile usafoundings
save `usafoundings'
clear
import excel "Data/NGOs_AllCountries", sheet("Sheet1") firstrow
keep if CountryTerritoryHQ=="USA"
drop if Founded==.
foreach num of numlist 1900/2022 {
	gen v_`num'=`num'
	} 
foreach x of varlist v_1900-v_2022 {
    replace `x'=. if `x'<Founded
    }
foreach x of varlist v_1900-v_2022 {
    replace `x'=0 if `x'==.
    }
foreach x of varlist v_1900-v_2022 {
    replace `x'=1 if `x'>0
    }
gen id=_n
reshape long v_, i(id) j(year)	
drop id
rename v_ total
collapse (sum) total, by(year)
replace year=year+1
rename total density
gen density_sq=density^2
merge 1:1 year using `usafoundings'
drop _merge
tempfile yiomerged
save `yiomerged'
clear
import excel "Data/gallup-trend.xlsx", sheet("Sheet0") firstrow
rename NormalizedResponseCategories active
rename G stay_out
drop in 1 
destring active stay_out, replace
gen year=year(StartDate)
collapse active stay_out, by(year)
replace year=year+1
tsset year
tsfill
ipolate active year, gen(iactive)
label variable iactive "Public Internationalism"
sort year
merge 1:1 year using `yiomerged'
drop _merge
tempfile yiomerged
save `yiomerged'
clear
import excel "Data/us_foreignaid_greenbook.xlsx", sheet("Economic and Military Aid") firstrow
keep if AssistanceCategory=="Economic"
collapse (sum) ObligationsConstantDollars, by(FiscalYear)
rename FiscalYear year
replace year=year+1
gen lnaid=log(ObligationsConstantDollars+1)
merge 1:1 year using `yiomerged'
generate aid_billions=ObligationsConstantDollars/1000000000
label variable aid_billions "Billions of Constant 2017 Dollars"
keep if year>1959 & year<2021
egen stddensity=std(density)
egen stddensity_sq=std(density_sq)
label variable stddensity "Density"
label variable stddensity_sq "Density Squared"
label variable lnaid "U.S. Aid"
reg count lnaid iactive
estimates store Baseline
reg count lnaid iactive stddensity stddensity_sq
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/foundings_reg_19602020.pdf", as(pdf) replace

/*Robustness for Figure 3.7*/
/*Table for Figure 3.7*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
drop if ein==.
drop if fisyr<1992 | fisyr>2019
sort ein fisyr
by ein, sort: gen years=fisyr[_n+1]-fisyr
generate fail=0
replace fail=1 if years>2
by ein, sort: generate totalobs=_N
sort ein fisyr
by ein, sort: generate id=_n
replace fail=1 if totalobs==id & fisyr<2017
label variable fail "=1 if the organization fails that year"
generate foundingyear=int(ruledate/100)
destring fisyr, replace
generate age=fisyr-foundingyear+1
replace age=. if age>1900
label variable age "organizational age in years (IRS ruling = age 1)"
keep ein fisyr field fail totrev age
replace totrev=0 if totrev<0
gen lnrev=log(totrev+1)
rename fisyr year
sort ein year
by ein: gen lastobs = _n == _N
replace fail=0 if fail==1 & lastobs==0
sort field year
tempfile failures
save `failures'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
generate density_sq=density*density
collapse (mean) density density_sq, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace density_sq=0 if _merge==2
replace year=year+1 /*to lag density*/
drop if year>2019
drop _merge
sort field year
merge field year using `failures'
egen stddensity=std(density)
egen stddensity_sq=std(density_sq)
egen stdage=std(age)
label variable stddensity "Density"
label variable stddensity_sq "Density Squared"
label variable stdage "INGO Age"
label variable lnrev "INGO Revenue"
reg fail lnrev stdage i.year if year<2017, vce(cluster field)
outreg2 using "Tables/figure3-7.tex", ctitle(Baseline Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) replace
reg fail lnrev stdage stddensity stddensity_sq i.year if year<2017, vce(cluster field)
outreg2 using "Tables/figure3-7.tex", ctitle(Full Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) append

/*Logit instead of OLS*/
logit fail lnrev stdage i.year if year<2017, vce(cluster field)
estimates store Baseline
logit fail lnrev stdage stddensity stddensity_sq i.year if year<2017, vce(cluster field)
estimates store Full
coefplot Baseline Full, keep(*lnrev *stdage *stddensity *stddensity_sq) eform xline(1) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/failures_logit.pdf", as(pdf) replace

